// 在vite中自动生成路由
import { createRouter, createWebHistory } from 'vue-router'

const pages = import.meta.glob('/src/views/**/page.js', {
  eager: true, // 获取导出结果
  import: 'default',
})
const componerts = import.meta.glob('/src/views/**/index.vue', {
  eager: true, // 获取导出结果
  import: 'default',
})

const routes = Object.entries(pages).forEach(([path, meta]) => {
  path = path.replace('/src/views', '').replace('/page.js', '') || '/'
  const name = path.split('/').filter(Boolean).join('-') || 'index'
  const compPath = path.replace('/page.js', '')
  return {
    path,
    name,
    component: componerts[compPath],
    meta,
  }
})

export const router = createRouter({
  history: createWebHistory(),
  routes,
})
